<template>
  <div>
    <el-card class="elcard">
      <el-form>
        <el-form-item>
          <el-input width="60px" class="input-width" />
          <el-button icon="el-icon-search" type="primary">查询</el-button>
          <el-button>清空</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="showAddRoleName"
            >添加角色</el-button
          >
          <el-button type="danger">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table style="width: 100%" border class="Spu-table" :data="roleList">
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" type="index" width="60" center />
        <el-table-column label="角色名称" prop="roleName" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="info"
              icon="el-icon-user"
              size="small"
              @click="goRoleAuth(row)"
            />
            <el-button
              type="warning"
              icon="el-icon-edit-outline"
              size="small"
            />
            <el-button type="danger" icon="el-icon-delete" size="small" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="limit"
        layout=" prev, pager, next, jumper ,->,total, sizes"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="pagination-div"
      />
      <!-- 显示添加用户名 -->
      <el-dialog title="添加角色" :visible.sync="DialogVisible" width="30%">
        <el-form>
          <el-form-item label="角色名">
            <el-input v-model="roleName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="DialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRoleName">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { reqRoleList } from '@/api/role'
import { reqAddRoleName } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      DialogVisible: false,
      roleName: '',
      // 当前页码
      page: 1,
      // 每页多少条
      limit: 3,
      total: 5
    }
  },
  mounted() {
    this.getRoleList(this.page, this.limit)
  },
  methods: {
    // 获取分页列表
    async getRoleList(page, limit) {
      let result = await reqRoleList(page, limit)
      this.roleList = result.data.items
      this.total = result.data.total
    },

    // 分配用户角色列表
    goRoleAuth(row) {
      this.$router.push({
        name: 'RoleAuth',
        query: {
          id: row.id,
          roleName: row.roleName
        }
      })
    },

    // 每页多少条
    handleSizeChange(size) {
      console.log(size)
      this.getRoleList(this.page, size)
    },
    // 当前页码
    handleCurrentChange(current) {
      this.getRoleList(current, this.limit)
    },

    // 点击展示角色
    showAddRoleName() {
      this.DialogVisible = true
    },

    // 点击添加角色
    async addRoleName() {
      await reqAddRoleName(this.roleName)
      this.$message.success('添加角色成功')
      this.getRoleList(this.page, this.limit)
      this.DialogVisible = false
      this.roleName = ''
    }
  }
}
</script>

<style scoped>
.elcard {
  margin: 20px;
}
.input-width {
  width: 200px;
  margin-right: 10px;
}

.pagination-div {
  margin-top: 20px;
}
</style>
